﻿using System.Collections.Generic;
using System.Linq;
using ZilLion.Service.Core.DapperWrapper;
using ZilLion.Service.Core.DapperWrapper.SqlServer;
using ZilLion.Service.ZilLionOss.Domain.Entitys;
using ZilLion.Service.ZilLionOss.Domain.Repositorys.Interface;

namespace ZilLion.Service.ZilLionOss.Domain.Repositorys
{
    public class OssCompanyInfoReponsitory : SqlServerRespository<OssCompanyInfoEntity>, IOssCompanyInfoReponsitory
    {
        public OssCompanyInfoReponsitory()
        {
            Context = new OssContext();
        }

        public IList<OssCompanyInfoEntity> GetAllCompany()
        {
            return GetAll().ToList();
        }

        public OssCompanyInfoEntity GetCompanyByKey(string key)
        {
            var querysql = DapperHelper.CreateColSqlByMap<OssCompanyInfoEntity>();
            var sql = $@"SELECT {querysql} FROM oss_company_info
            WHERE zlcompanykey=@companyKey";
            var result = GetList(sql, new {companyKey = key}).FirstOrDefault();
            return result;
        }

        public void AddCompany(OssCompanyInfoEntity entity, UnitOfWork unit = null)
        {
            Add(entity, unit);
        }

        public void EditCompany(OssCompanyInfoEntity entity, UnitOfWork unit = null)
        {
            Modify(entity, unit);
        }

        public void RemoveCompany(OssCompanyInfoEntity entity, UnitOfWork unit = null)
        {
            Remove(entity, unit);
        }

        public void RemoveCompany(string companykey, UnitOfWork unit = null)
        {
            DapperHelper.Execute("delete from oss_company_info where zlcompanykey=@companykey", Context, unit,
                new { companykey });
        }
    }
}